package cn.cas.mango.service;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
@Data
public class ConfigService {
  @Value("${elasticsearch.real.index}")
  private String realIndex;

  @Value("${elasticsearch.alias.index}")
  private String aliasIndex;

  @Value("${elasticsearch.index}")
  private String index;

  @Value("${elasticsearch.material.index}")
  private String materialIndex;
  @Value("${person.total.index}")
  private String biPersonTotalIndex;

  @Value("${elasticsearch.hotsport.index}")
  private String hotspotIndex;

  @Value("${history.path}")
  private String historyPath;
  @Value("${fragment.size}")
  private Integer fragmentSize;
  @Value("${elasticsearch.host}")
  private String host;
  @Value("${elasticsearch.port}")
  private int port;
  @Value("${elasticsearch.username:-}")
  private String username;
  @Value("${elasticsearch.password:-}")
  private String password;
  @Value("${default.hits.count}")
  private int hitsCount;
  @Value("${web.url}")
  private String mediaUrl;
  @Value("${boost}")
  private float boost;
  @Value("${analyzer}")
  private String analyzer;

  @Value("${getSim.likeDegree}")
  private Double simDegree;

  @Value("${elasticsearch.pageSize}")
  private int pageSize;

  @Value("${elasticsearch.hotPageSize}")
  private int hotPageSize;

  @Value("${elasticsearch.materialPageSize}")
  private int materialPageSize;

  @Value("${exact.search.special.symbol}")
  private String exactSearchSpecialSymbol;
  @Value("${elasticsearch.websiteSize}")
  private Integer websiteSize;
  @Value("${exact_analyzer}")
  private String exactAnalyzer;
  @Value("${elasticsearch.fuzzy.value}")
  private String fuzzyValue;
  @Value("${outer.address}")
  private String outerAddress;

  @Value("${topic.likeDegree}")
  private String topicLikeDegree;

  @Value("${getSim.sim.field}")
  private String simField;

  @Value("${person.index}")
  private String personIndex;

  @Value("${mlt.min.term.freq}")
  private Integer mltMinTermFreq;

  @Value("${mlt.max.query.terms}")
  private Integer mltMaxQueryTerms;

  @Value("${mlt.min.doc.freq}")
  private Integer mltMinDocFreq;

  @Value("#{'${elasticsearch.material.department}'.split(',')}")
  private List<String> materialDepartment;

  // 整编任务保存的url
  @Value("${temporary.save.news.url}")
  private String temporarySaveNewsUrl;

  @Value("${recommend.field.type}")
  private String recommendFieldType;

  private int recommendKeywordsNum = 12;

  @Value("${topic.recommend.mode}")
  private String topicRecommendMode;

  @Value("${hot.like.degree.times}")
  private Double hotDegreeTimes;

  @Value("${hot.like.degree.days}")
  private int hotDegreeDays;

  private Map<String, String> areaCastMap = new HashMap<>();
  {
    areaCastMap.put("经济领域", "0002");
    areaCastMap.put("核领域", "0003");
    areaCastMap.put("政治领域", "0004");
    areaCastMap.put("军事领域", "0005");
    areaCastMap.put("科技领域", "0006");
    areaCastMap.put("国土领域", "0007");
    areaCastMap.put("社会领域", "0008");
    areaCastMap.put("文化领域", "0009");
    areaCastMap.put("网络领域", "0010");
    areaCastMap.put("生态领域", "0011");
    areaCastMap.put("资源领域", "0012");
    areaCastMap.put("金融领域", "0013");
    areaCastMap.put("太空领域", "0014");
    areaCastMap.put("极地领域", "0015");
    areaCastMap.put("深海领域", "0016");
    areaCastMap.put("生物领域", "0017");
    areaCastMap.put("海外利益领域", "0018");
    areaCastMap.put("数据领域", "0019");
    areaCastMap.put("人工智能领域", "0020");
    areaCastMap.put("粮食领域", "0021");
  }

  private Map<String, String> number2AreaCast = new HashMap<>();
  {
    number2AreaCast.put( "0002", "经济领域");
    number2AreaCast.put( "0003", "核领域");
    number2AreaCast.put( "0004", "政治领域");
    number2AreaCast.put( "0005", "军事领域");
    number2AreaCast.put( "0006", "科技领域");
    number2AreaCast.put( "0007", "国土领域");
    number2AreaCast.put( "0008", "社会领域");
    number2AreaCast.put( "0009", "文化领域");
    number2AreaCast.put( "0010", "网络领域");
    number2AreaCast.put( "0011", "生态领域");
    number2AreaCast.put( "0012", "资源领域");
    number2AreaCast.put( "0013", "金融领域");
    number2AreaCast.put( "0014", "太空领域");
    number2AreaCast.put( "0015", "极地领域");
    number2AreaCast.put( "0016", "深海领域");
    number2AreaCast.put( "0017", "生物领域");
    number2AreaCast.put( "0018", "海外利益领域");
    number2AreaCast.put( "0019", "数据领域");
    number2AreaCast.put( "0020", "人工智能领域");
    number2AreaCast.put( "0021", "粮食领域");
  }

  private Map<String, String> nm2Department = new LinkedHashMap<String, String>(){{
    put("01", "外交部");
    put("02", "北京网信办");
    put("03", "公安部");
    put("04", "国家民委");
    put("05", "商务部");
    put("06", "国家安全部");
    put("07", "国家统计局");
    put("08", "海关总署");
    put("09", "应急管理部");
    put("10", "国家卫生健康委");
  }};

  private Map<String, String> department2Nm = new LinkedHashMap<String, String>(){{
    for (String key: nm2Department.keySet()) {
      put(nm2Department.get(key), key);
    }
  }};

  private Map<String, String> nm2Province = new LinkedHashMap<String, String>(){{

    put("42", "湖北");
    put("11", "北京");
    put("12", "天津");
    put("13", "河北");
    put("14", "山西");
    put("15", "内蒙古");
    put("21", "辽宁");
    put("22", "吉林");
    put("23", "黑龙江");
    put("31", "上海");
    put("32", "江苏");
    put("33", "浙江");
    put("34", "安徽");
    put("35", "福建");
    put("36", "江西");
    put("37", "山东");
    put("41", "河南");
    put("43", "湖南");
    put("44", "广东");
    put("45", "广西");
    put("46", "海南");
    put("51", "重庆");
    put("52", "四川");
    put("53", "贵州");
    put("54", "云南");
    put("55", "西藏");
    put("61", "陕西");
    put("62", "甘肃");
    put("63", "青海");
    put("64", "宁夏");
    put("65", "新疆");
    put("66", "新疆兵团");
    put("91", "信息技术中心");
  }};

  private Map<String, String> province2Nm = new LinkedHashMap<String, String>(){{
    put("湖北", "42");
    put("北京", "11");
    put("天津", "12");
    put("河北", "13");
    put("山西", "14");
    put("内蒙古", "15");
    put("辽宁", "21");
    put("吉林", "22");
    put("黑龙江", "23");
    put("上海", "31");
    put("江苏", "32");
    put("浙江", "33");
    put("安徽", "34");
    put("福建", "35");
    put("江西", "36");
    put("山东", "37");
    put("河南", "41");
    put("湖南", "43");
    put("广东", "44");
    put("广西", "45");
    put("海南", "46");
    put("重庆", "51");
    put("四川", "52");
    put("贵州", "53");
    put("云南", "54");
    put("西藏", "55");
    put("陕西", "61");
    put("甘肃", "62");
    put("青海", "63");
    put("宁夏", "64");
    put("新疆", "65");
    put("新疆兵团", "66");
    put("信息技术中心", "91");

  }};

  private final Map<Integer, String> colorMap = new HashMap<Integer, String>(){{
    put(1, "#337AB7");
    put(2, "#54BA9A");
    put(3, "#FF7D3D");
    put(4, "#E55050");
  }};

  Map<String, String> webSiteNameMap = new HashMap<String, String>(){{
    put("OVERSEAS", "境外媒体");
    put("HMT", "港澳台媒体");
    put("WEBSITE", "网络媒体");
    put("GOV", "官方媒体");
    put("ANTI", "反动媒体");
  }};

  public String getWebSiteNameMap(String key) {
    if (webSiteNameMap.containsKey(key)) {
      return webSiteNameMap.get(key);
    }
    return "未知";
  }


  private Map<String, String> webSiteTypeName = new HashMap<>();
  {
    webSiteTypeName.put("OVERSEAS", "境外媒体");
    webSiteTypeName.put("HMT", "港澳台媒体");
    webSiteTypeName.put("WEBSITE", "网络媒体");
    webSiteTypeName.put("GOV", "官方媒体");
    webSiteTypeName.put("ANTI", "反动媒体");
  }

  public String getWebSiteTypeName(String key) {
    return webSiteTypeName.get(key) == null ? "未知" : webSiteTypeName.get(key);
  }

  private int newsDetailShowMaxKeywords = 20;

  private Set<String> blackList = new HashSet<>();
  {
    try {
      blackList.addAll(getBlackList1("blacklist.txt"));
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  public List<String> getBlackList1(String filePath) throws IOException {
    FileInputStream fis = new FileInputStream(filePath);
    List<String> blackLists = new ArrayList<>();
    //Construct BufferedReader from InputStreamReader
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    String line = null;
    while ((line = br.readLine()) != null) {
      blackLists.add(line.trim());
    }
    br.close();
    fis.close();
    return blackLists;
  }

}
